package com.techfort.affs;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.techfort.affs.mongoclient.MongolabClient;

public class ShareAffs extends Activity {

	private StoredAccountAdapter db;
	
	MongolabClient client;
	ListView  affslist;
	EditText sharemail;
	Button sharebutton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.share);
		
		sharemail = (EditText) findViewById(R.id.share_email);
		sharebutton = (Button) findViewById(R.id.sharebutton);
		sharebutton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
				Log.i("INFO", "clicked share...");
				if(affslist!=null){
					startActivity(new Intent(ShareAffs.this, ShareAffs.class));
				}
				
			}
		});
		
		db = new StoredAccountAdapter(getApplicationContext());
		db.open();
    	client = new MongolabClient("affs","users","4ead9de7e4b0e737eda86573");
		
		JSONObject joe = getCurrentUser();
		try {
			JSONArray array = joe.getJSONArray("affs");
			affslist = (ListView) findViewById(R.id.sharelist);
			CheckBoxAdapter cba = new CheckBoxAdapter(ShareAffs.this, R.layout.checktext, convertJSONArrayToList(array));
			affslist.setAdapter(cba);
			
			
		} catch (JSONException e) {

			e.printStackTrace();
		}
			
	}
	
	
	public List<String> convertJSONArrayToList(JSONArray array) throws JSONException{
    	
    	List<String> stringarray = new ArrayList<String>();
    	for(int i = 0; i < array.length(); i++){
    		stringarray.add(array.getString(i));
    	}
    	return stringarray;
    	
    }
	
	private class CheckBoxAdapter extends ArrayAdapter<String>{

		List<String > affs;
		
		public CheckBoxAdapter(Context context, int textViewResourceId,
				List<String> objects) {
			super(context, textViewResourceId, objects);
			affs = objects;
			
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if(v==null){
				LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = li.inflate(R.layout.checktext, null);
			}
			String aff = affs.get(position);
			TextView tv = (TextView) v.findViewById(R.id.afftext);
			tv.setText(aff);
			
			return v;
		}
		
	}
	
	
    private JSONObject getCurrentUser(){
    	List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		qparams.add(new BasicNameValuePair("q","{\"username\":\"" + db.getAccount() + "\"}"));
		
		JSONObject user = client.findOne(qparams);
		return user;
    }
}
